package com.txzt.gaea.upgrade.result.service.impl;

import com.txzt.gaea.business.sequence.service.ISequenceService;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.ResultStatus;
import com.txzt.gaea.common.enums.FlowFlagEnum;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.exception.CustomException;
import com.txzt.gaea.flow.model.FlowBizStep;
import com.txzt.gaea.flow.service.FlowService;
import com.txzt.gaea.upgrade.result.domain.model.ResultInfo;
import com.txzt.gaea.upgrade.result.service.IResultInfoService;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;


@Service
public class ResultInfoServiceImpl extends BaseServiceImpl<ResultInfo> implements IResultInfoService {

    private final static String SEPARATOR = "-";

    @Autowired
    private FlowService flowService;
    @Autowired
    private ISequenceService sequenceService;

    /**
     * 成果申请 - 保存/提交
     * @param resultInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addResultInfo(ResultInfo resultInfo) {
        Calendar calendar = Calendar.getInstance();
        // 获取当前年
        int year = calendar.get(Calendar.YEAR);
        Integer sequence = sequenceService.getSequence("result_info", "result_no");
        String budgetNo = year + SEPARATOR + "CGGL" + SEPARATOR + String.format("%03d", sequence);
        resultInfo.setResultNo(budgetNo);
        resultInfo.setResultStatus(ResultStatus.INIT.getCode());
        resultInfo.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        int count = sqlManager.insertTemplate(resultInfo,true);
        Long flowId = null;
        if (resultInfo.getIsCheckSubmit()) {
            //开启流程
            flowId = flowService.startFlowUpgrade(resultInfo.getId(), "tb_result_info","result_status",
                    ResultStatus.BACK.getCode(),ResultStatus.PASS.getCode(), ResultStatus.REJECT.getCode(),
                    ResultStatus.INIT.getCode(),FlowFlagEnum.RESULT_INFO.getCode(), resultInfo.getDeptId());
        }
        //添加流程id
        if (null != flowId) {
            ResultInfo target = new ResultInfo();
            target.setId(resultInfo.getId());
            target.setFlowId(flowId);
            target.setResultStatus(ResultStatus.CHECK.getCode());
            sqlManager.updateTemplateById(target);
        }
        return count;
    }



    /**
     * 成果申请 - 编辑/提交
     * @param resultInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int editResultInfo(ResultInfo resultInfo) {
        if (resultInfo.getIsCheckSubmit()) {
            if (null == resultInfo.getFlowId()) {
                //开启流程
                Long flowId = flowService.startFlowUpgrade(resultInfo.getId(), "tb_result_info","result_status",
                        ResultStatus.BACK.getCode(),ResultStatus.PASS.getCode(), ResultStatus.REJECT.getCode(),
                        ResultStatus.INIT.getCode(),FlowFlagEnum.RESULT_INFO.getCode(), resultInfo.getDeptId());
                resultInfo.setFlowId(flowId);
            } else if (resultInfo.getResultStatus().equals(ResultStatus.BACK.getCode())) {
                flowService.nextStepOnlyForBack(resultInfo.getFlowId());
            }
            resultInfo.setResultStatus(ResultStatus.CHECK.getCode());
        }
        return sqlManager.updateTemplateById(resultInfo);
    }


    /**
     * 成果申请 - 撤回
     * @param resultInfo
     * @return
     */
    @Override
    public int revocationResultInfo(ResultInfo resultInfo) {
        if (!resultInfo.getResultStatus().equals(ResultStatus.CHECK.getCode())) {
            throw new CustomException("非审核中数据不可撤回！");
        }
        List<FlowBizStep> stepList = sqlManager.lambdaQuery(FlowBizStep.class)
                .andEq(FlowBizStep::getFlowBizId, resultInfo.getFlowId())
                .orderBy("step_no asc").select();
        if (0 == stepList.size()) {
            throw new CustomException("撤回异常！");
        }else {
            if (null == stepList.get(0).getStepStatus()) {
                ResultInfo target = new ResultInfo();
                target.setId(resultInfo.getId());
                target.setResultStatus(ResultStatus.INIT.getCode());
                return sqlManager.updateTemplateById(target);
            }else {
                throw new CustomException("数据已审核不可撤回！");
            }
        }
    }

    @Override
    public PageQuery<ResultInfo> queryInfo(PageQuery<ResultInfo> query) {
        return sqlManager.pageQuery("resultInfo.queryInfo",ResultInfo.class,query);
    }

    /**
     * 查询待审核数量(成果申请)
     * @param resultInfo
     * @return
     */
    @Override
    public Long listCheckCount(ResultInfo resultInfo) {
        List<Long> list = sqlManager.select("resultInfo.selectResultInfoCheckCount", Long.class, resultInfo);
        return list.get(0);
    }
}
